package com.douma.linkedlist;


public class RemoveNthNode {

    int curr = 0;
    // 删除第 n 个节点，并返回删除节点后的链表的头节点
    public ListNode removeNth(ListNode head, int n) {
        if (head == null) return null;

        curr++;
        if (curr == n) {
            return head.next;
        }

        ListNode ret = removeNth(head.next, n);

        head.next = ret;
        return head;
    }


    // 删除倒数第 n 个节点，并返回删除节点后的链表的头节点
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) return null;

        ListNode ret = removeNthFromEnd(head.next, n);
        head.next = ret;

        curr++;
        if (curr == n) {
            return head.next;
        }

        return head;
    }
}
